package com.hdaccp.mybatis.controller;

import com.hdaccp.mybatis.dao.UsersDao;
import com.hdaccp.mybatis.entity.Users;
import com.hdaccp.mybatis.util.BaseDao;
import org.apache.ibatis.session.SqlSession;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet(urlPatterns = "/user")
public class UserServlet extends HttpServlet {
    private UsersDao usersDao ;
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        req.setCharacterEncoding("utf-8");
        String method = req.getParameter("method") ;
        if("query".equals(method)){
            this.doQuery(req,resp) ;
        }
    }

    protected void doQuery(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        BaseDao baseDao = new BaseDao() ;
        SqlSession session = baseDao.getSession() ;

        List<Users> list = session.getMapper(UsersDao.class).query() ;
        req.setAttribute("list",list);
        req.getRequestDispatcher("userlist.jsp").forward(req,resp);
        session.close();
    }

    protected void doAdd(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    }

    protected void doDel(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    }

    protected void doUpdate(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    }
}
